<template>
  <j-modal
    :title="title"
    :width="1400"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    :dialog-style="{ top: '20px' }"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-card :bordered="false">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :xl="4" :lg="7" :md="8" :sm="24">
              <a-form-item label="券名称">
                <a-input placeholder="" v-model="queryParam.actNm" allowClear></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="4" :lg="7" :md="8" :sm="24">
              <a-form-item label="优惠类型">
                <a-select v-model="queryParam.couponType" allowClear>
                  <a-select-option value="mj">满减</a-select-option>
                  <a-select-option value="zk">折扣</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="5" :lg="7" :md="8" :sm="24">
              <a-form-item label="有效期">
                <a-range-picker
                  :ranges="{ 今天: [moment(), moment()], 本月: [moment().startOf('month'), moment().endOf('month')] }"
                  v-model="shelevsDateRange"
                  @change="handleDateChange"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="3" :lg="7" :md="8" :sm="24">
              <a-form-item label="是否启用">
                <a-select v-model="queryParam.bizStatus" allowClear>
                  <a-select-option key="" value="1">是</a-select-option>
                  <a-select-option key="" value="0">否</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="4" :lg="7" :md="8" :sm="24">
              <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!-- table区域-begin -->
      <div>
        <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          class="j-table-force-nowrap"
          :rowSelection="{selectedRowKeys, onChange: onSelectChange, type: multiple ? 'checkbox':'radio'}"
          @change="handleTableChange"
        >
          <span slot="bizStatus" slot-scope="text">
            {{ text ? '是' : '否' }}
          </span>
        </a-table>
      </div>
      <!-- table区域-end -->
    </a-card>
    </a-spin>
  </j-modal>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import moment from 'moment'
import { queryDepartTreeList1 } from '@/api/api'

export default {
  name: 'SelectCouponList',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      title: '选择优惠券',
      disableMixinCreated: true,
      shelevsDateRange: [],
      departTreeOptions: [],
      confirmLoading: false,
      multiple:true,
      visible: false,
      fieldNames: { label: 'title', value: 'value', children: 'children' },
      moment,
      // 表头
      columns: [
        {
          title: '券编码',
          align: 'center',
          dataIndex: 'actCd',
        },
        {
          title: '券名称',
          align: 'center',
          dataIndex: 'actNm',
        },
        {
          title: '优惠类型',
          align: 'center',
          dataIndex: 'couponType',
          customRender: (text, record) => {
            if (text === 'zk') {
              return '折扣'
            }
            if (text === 'mj') {
              return '满减'
            }
          },
        },
        {
          title: '券有效期',
          align: 'center',
          dataIndex: 'couponValidTime',
        },
        {
          title: '适用门店',
          align: 'center',
          dataIndex: 'scopeShopNames',
          customRender: (text) => {
            return text + '/全部门店'
          },
        },
        {
          title: '发券总数',
          align: 'center',
          dataIndex: 'distTotal',
        },

        {
          title: '领取数量',
          align: 'center',
          dataIndex: 'getCount',
        },
        {
          title: '剩余数量',
          align: 'center',
          dataIndex: 'remainCount',
          customRender: (text, record) => {
            let remainCount = 0
            remainCount = record.distTotal - record.getCount
            return remainCount
          },
        },
        {
          title: '是否启用',
          align: 'center',
          dataIndex: 'bizStatus',
          scopedSlots: { customRender: 'bizStatus' },
        },
      ],
      url: {
        list: '/marketing/posMarketCoupon/list',
      },
    }
  },
  created() {},
  methods: {
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      console.log('this.selectionRows', this.selectionRows)
      if(this.selectionRows.length == 0){
        this.$message.warning('请选择优惠券')
        return false
      }else if(this.selectionRows.length > 5){
        this.$message.warning('最多选择5张优惠券')
        return false
      }
      
      this.$emit('ok', this.selectionRows)
      this.onClearSelected()
      this.visible = false
    },
    search(orgIds) {
      this.queryParam.bizStatus = '1'
      if(orgIds){
        this.queryParam.selecteddeparts = [orgIds, 0]
      }else{
        this.queryParam.selecteddeparts = []
      }
      this.searchQuery()
    },
    searchQuery() {
      let selecteddeparts = this.queryParam.selecteddeparts
      if (selecteddeparts && selecteddeparts.length == 2) {
        this.queryParam.orgScope = selecteddeparts[0]
        this.queryParam.shopScope = selecteddeparts[1]
      }
      this.loadData(1)
    },
    loadDepartTree() {
      queryDepartTreeList1({ orgTypes: '2' }).then((res) => {
        if (res.success) {
          this.departTreeOptions = res.result
          this.departTreeOptions.forEach((x) => {
            x['children'] = [{ title: '全部门店', value: '0' }]
          })
        }
      })
    },
    onDepartChange(value, selectedOptions) {
      console.log('value:', value)
      console.log('selectedOptions:', selectedOptions)
      this.orgIds = value && value.length > 0 ? value[0] : ''
    },
    handleDateChange(date, dateString) {
      this.queryParam.startTime = dateString[0]
      this.queryParam.endTime = dateString[1]
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>